package com.itheima.web.controller;



import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Company;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.CompanyService;
import com.itheima.service.impl.CompanyServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * 该Servlet处理说有的CRUD操作
 * 分页查询: http://localhost:8080/heima_mm/store/company/findByPage?currentPage=1&pageSize=5;
 * 新增:http://localhost:8080/heima_mm/store/company/save
 * 新增:http://localhost:8080/heima_mm/store/company/edit
 * 新增:http://localhost:8080/heima_mm/store/company/delete
 *
 */
@WebServlet("/store/company/*")
public class CompanyServlet extends BaseServlet {

       private CompanyService companyService = BeanFactory.getBean("companyService", CompanyService.class);
    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {
            //1. 获取请求参数并封装
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            //2.调用service层方法,编辑数据

            companyService.delete(ids);
            //3.响应结果
            return new Result(true,MessageConstant.DELETE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_COMPANY_FAIL);
        }

    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            //1. 获取请求参数并封装
            Company company = BeanUtil.fillBeanFromJson(request, Company.class);
            //2.调用service层方法,编辑数据
            
            companyService.update(company);
            //3.响应结果
            return new Result(true,MessageConstant.EDIT_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_COMPANY_FAIL);
        }


    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
            
        try {
            //获取请求参数 id
            String id = request.getParameter("id");
            //调用service,根据id查询信息

            Company company = companyService.findById(id);
            //响应给过到客户端
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,company);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_COMPANY_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1. 获取请求参数并封装
            Company company = BeanUtil.fillBeanFromJson(request, Company.class);
            //2.调用service层方法,保存数据
            companyService.save(company);
            //3.响应结果
            return new Result(true,MessageConstant.SAVE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_COMPANY_FAIL);
        }
    }

    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //1.获取请求参数 currentPage和pageSize
            String currentPageSrt = request.getParameter("currentPage");
            String pageSizeSrt = request.getParameter("pageSize");
            //2.非空校验并赋默认值
            int currentPage=1,pageSize=5;
            if (!StringUtils.isBlank(currentPageSrt)){
                 currentPage = Integer.parseInt(currentPageSrt);
            }
            if (!StringUtils.isBlank(pageSizeSrt)){
                pageSize = Integer.parseInt(pageSizeSrt);
            }
            //3.调用service层方法,执行查询pageInfo
            PageInfo<Company> info = companyService.findByPage(currentPage, pageSize);
            //4.响应结果
            return new Result(true, MessageConstant.QUERY_COMPANY_SUCCESS,info);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_COMPANY_FAIL);
        }
    }

    /**
     * 查询所有信息的方法
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {

             //调用service,根据id查询信息
            List<Company> list = companyService.findAll();
            //响应给过到客户端
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_COMPANY_FAIL);
        }
    }


}
